package com.sudo.api._admin;

import com.sudo.common.annotation.ApiIdempotent;
import com.sudo.common.annotation.ApiRateLimiter;
import com.sudo.common.annotation.ApiAuthorization;
import com.sudo.common.utils.HttpUserInfoUtil;
import com.sudo.common.utils.JsonRes;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.service.admin.service.UserFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 为道日损 2020-06-21 12:21
 * webSite：https://www.sudo.ren
 */
@Slf4j
@Api(value = "上传文件",tags = {"上传文件相关接口"})
@RestController
@RequestMapping("fileRun")
public class FileController extends AdminBaseController {

    @Resource
    private UserFileService userFileService;

    @ApiRateLimiter(times = 50,second = 10L)
    @ApiIdempotent
    @ApiAuthorization(menu = "upload_file",button = "upload_allFiles")
    @ApiOperation(value = "上传普通文件",notes = "上传普通文件",httpMethod = "POST")
    @PostMapping("uploadCommonFile")
    public JsonRes uploadCommonFile(MultipartFile file,
                                    HttpServletRequest request) {
        if (null == file) {
            return JsonRes.errorMsg("文件不能为空");
        }
        if (StringUtils.isBlank(file.getOriginalFilename())) {
            return JsonRes.errorMsg("文件格式不正确");
        }
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        int affectCount = userFileService.uploadCommonFile(file, sessionUser);
        return JsonRes.judge(affectCount,"文件上传成功","文件上传失败");
    }

    @ApiIdempotent
    @ApiOperation(value = "上传我的头像",notes = "上传我的头像",httpMethod = "POST")
    @PostMapping("uploadMyLogo")
    public JsonRes uploadMyLogo(MultipartFile avatar_file,
                                    HttpServletRequest request) {
        if (null == avatar_file) {
            return JsonRes.errorMsg("文件不能为空");
        }
        if (StringUtils.isBlank(avatar_file.getOriginalFilename())) {
            return JsonRes.errorMsg("文件格式不正确");
        }
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        String logicalPosition = userFileService.uploadUseLogo(avatar_file, request, sessionUser);
        if (logicalPosition == null){
            return JsonRes.errorMsg("上传头像失败");
        }
        return JsonRes.ok(logicalPosition);
    }

    @ApiOperation(value = "分页获取所有文件",notes = "分页获取所有文件",httpMethod = "GET")
    @GetMapping("getAllFilesByPage")
    public JsonRes getAllFilesByPage(@RequestParam Integer page,
                                  @RequestParam Integer pageSize,
                                  @RequestParam String username,
                                  @RequestParam String startDate,
                                  @RequestParam String endDate) {
        PagedGridResult pagedGridResult = userFileService.getAllFilesByPage(page, pageSize, username, startDate, endDate);
        return JsonRes.ok(pagedGridResult);
    }

    @ApiOperation(value = "分页获取我的文件",notes = "分页获取我的文件",httpMethod = "GET")
    @GetMapping("getMyFilesByPage")
    public JsonRes getMyFilesByPage(@RequestParam Integer page,
                                  @RequestParam Integer pageSize,
                                  @RequestParam String startDate,
                                  @RequestParam String endDate,
                                    HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        PagedGridResult pagedGridResult = userFileService.getAllFilesByPage(page, pageSize, sessionUser, startDate, endDate);
        return JsonRes.ok(pagedGridResult);
    }

}
